/**********************************************************************************************/
// dgt_DoControl.cpp
//
// 
// Copyright Diamant Group 2007
/**********************************************************************************************/

#include "dgt_DoControl.h"
#include <wx/image.h>
#include <wx/dc.h>
#include <wx/pen.h>
#include <wx/file.h>


/**********************************************************************************************/
BEGIN_EVENT_TABLE (dgt_DoControl, wxControl)
	EVT_LEFT_DOWN (dgt_DoControl::OnLeftDown)
	EVT_MOTION (dgt_DoControl::OnMove)
	EVT_LEFT_UP (dgt_DoControl::OnLeftUp)
	EVT_PAINT (dgt_DoControl::OnPaint)
	EVT_LEFT_DCLICK (dgt_DoControl::OnDClick)
	EVT_CONTEXT_MENU (dgt_DoControl::OnContextMenu)
	EVT_MENU (wxID_DELETE, dgt_DoControl::OnDeleteClick)
	EVT_ERASE_BACKGROUND(dgt_DoControl::OnEraseBackground)
	EVT_ENTER_WINDOW(dgt_DoControl::OnEnter)
	EVT_LEAVE_WINDOW(dgt_DoControl::OnLeave)
END_EVENT_TABLE ()

/**********************************************************************************************/
dgt_DoControl::dgt_DoControl(void)
{
}

/**********************************************************************************************/
dgt_DoControl::~dgt_DoControl(void)
{
}

/**********************************************************************************************/
dgt_DoControl::dgt_DoControl(wxWindow* parent, 
								 wxWindowID id, 
								 const wxPoint& pos, 
								 const wxSize& size, 
								 long style,
								 const wxValidator& validator )
: wxControl(parent, id, pos, size, style , validator)
, m_dragF( false ), m_drag(false), m_ldown(false), m_enter(false), m_top_marker(false),
m_bottom_marker(false)
{
}

/**********************************************************************************************/
//Painting control
void dgt_DoControl::OnPaint(wxPaintEvent& event)
{
		Refresh();
		wxPaintDC dc(this);
		
		if (m_enter)
		{
			dc.SetPen(*wxWHITE_PEN);
			dc.DrawRectangle(0,0,GetSize().GetX(), GetSize().GetY()  );
			wxPen *pen1 = new wxPen (*wxBLACK, 1, wxDOT);
			dc.SetPen(*pen1);
			dc.DrawRectangle(5 ,5, GetSize().GetX()-10, GetSize().GetY()-10 );

		}
		else
		{
			dc.SetPen(*wxWHITE_PEN);
			dc.DrawRectangle(0,0,GetSize().GetX(), GetSize().GetY()  );
			dc.SetBrush( *wxWHITE_BRUSH);
			wxPen *pen = new wxPen (*wxBLACK, 1);
			dc.SetPen(*pen);
			dc.DrawRectangle( 5, 5, GetSize().GetX()-10, GetSize().GetY()-10 );
		}
		if (m_top_marker) 
		{
			dc.SetPen(*wxWHITE_PEN);
			dc.DrawRectangle(0,0,GetSize().GetX(), GetSize().GetY()  );
			dc.SetBrush( *wxWHITE_BRUSH);
			wxPen *pen = new wxPen (*wxBLACK, 1);
			dc.SetPen(*pen);
			dc.DrawRectangle( 5, 5, GetSize().GetX()-10, GetSize().GetY()-10 );
			dc.SetBrush(*wxBLACK_BRUSH);
			dc.DrawRectangle(GetSize().GetX()/2-5,GetSize().GetY()-10, 10,10);
		}
		if (m_bottom_marker)
		{
			
			dc.SetPen(*wxWHITE_PEN);
			dc.DrawRectangle(0,0,GetSize().GetX(), GetSize().GetY()  );
			dc.SetBrush( *wxWHITE_BRUSH);
			wxPen *pen = new wxPen (*wxBLACK, 1);
			dc.SetPen(*pen);
			dc.DrawRectangle( 5, 5, GetSize().GetX()-10, GetSize().GetY()-10 );
			dc.SetBrush(*wxBLACK_BRUSH);
			dc.DrawRectangle(GetSize().GetX()/2-5,0, 10,10);
		}
}

/***********************************************************************************************/
wxSize dgt_DoControl::DoGetBestSize() const
{

	wxSize best;


	best.x = 100;
	best.y = 100;


	return best;


}

/**********************************************************************************************/
//Adding text to the control
void dgt_DoControl::AddText(wxString  string)
{

	wxStaticText * statText;
	this->Refresh(true,NULL);
	statText = new wxStaticText(this, wxID_ANY, string, wxPoint(30,30),
		wxDefaultSize, wxALIGN_RIGHT , wxEmptyString);

}

/**********************************************************************************************/
//On mouse double click event appears adding text dialog
void dgt_DoControl::OnDClick(wxMouseEvent& event)
{
	this->DestroyChildren();
	wxTextEntryDialog * TextDlg = new wxTextEntryDialog(this, _T("Please, enter text"), _T("Text dialog"),
		_T(""), wxOK | wxCANCEL | wxCENTRE, wxDefaultPosition);
	TextDlg->ShowModal();
	wxString string;
	string = TextDlg->GetValue();
	AddText(string);
}

/**********************************************************************************************/
//On mouse left button down event
void dgt_DoControl::OnLeftDown(wxMouseEvent &event )
{
	
	CaptureMouse(); 
	m_ldown = true;
	wxPoint pos = ClientToScreen(event.GetPosition()); 
	wxPoint origin = GetPosition(); 
	int dx =  pos.x - origin.x; 
	int dy = pos.y - origin.y;
	m_Point = wxPoint(dx, dy);
}

/**********************************************************************************************/
//It's a kind of moving control
void dgt_DoControl::OnMove(wxMouseEvent &event)
{
	wxPoint pt = event.GetPosition(); 
	int x1, y1;
	x1 = GetSize().GetX();
	y1 = GetSize().GetY();
	wxPoint pos = ClientToScreen(pt); 
	if ((pt.x>=x1/2-20)&&(pt.x<=x1/2+20)&&(pt.y>=y1-30)&&(pt.y<=y1+30)) m_top_marker = true;
	else if ((pt.x>=x1/2-20)&&(pt.x<=x1/2+20)&&(pt.y>=0)&&(pt.y<=10)) m_bottom_marker = true;
	else { m_bottom_marker = false; m_top_marker = false;}
	if (event.Dragging() && event.LeftIsDown()&&(!m_top_marker)&&(!m_bottom_marker)) 
	{ 
		//wxPoint pos = ClientToScreen(pt); 
		m_drag = true;
		Move(wxPoint(pos.x - m_Point.x, pos.y - m_Point.y)); 
	}
}

/**********************************************************************************************/
////On mouse left button up event
void dgt_DoControl::OnLeftUp(wxMouseEvent & event)
{
	
	
	if (HasCapture()) 
	{ 
		ReleaseMouse(); 
		
	}
	m_drag = false;
	m_ldown = false;
	wxPoint newpos;
	newpos.x = event.GetX();
	newpos.y = event.GetY();
	
	/*wxString stringg;
	stringg.Printf(_T("%d %d"), newpos.x, newpos.y);
	wxMessageBox(stringg);*/

}

/**********************************************************************************************/
//appending context menu when right button is pressed on the control
void dgt_DoControl::OnContextMenu(wxContextMenuEvent & event)
{

	wxPoint point = event.GetPosition();
	if (point.x == -1 && point.y == -1) {
		wxSize size = GetSize();
		point.x = size.x / 2;
		point.y = size.y / 2;
	} 
	else 
	{
		point = ScreenToClient(point);
	}
	wxMenu menu;

	menu.Append(wxID_DELETE, _T("&Delete"));
	menu.AppendSeparator();
	menu.Append(wxID_CANCEL, _T("Cancel"));

	this->PopupMenu(&menu, point.x, point.y);

}

/**********************************************************************************************/
//deleting control
void dgt_DoControl::OnDeleteClick(wxCommandEvent &event)
{
	this->Destroy();
}

/**********************************************************************************************/
//I suppose it avoids flickering but it doesn't
void dgt_DoControl::OnEraseBackground(wxEraseEvent &WXUNUSED(event)) 
{ 
	//don't do anything here 
} 

/**********************************************************************************************/
//Checks if mouse enters control and changes flag if it does
void dgt_DoControl::OnEnter( wxMouseEvent &event )
{

	m_enter = true;
	Refresh();
}

/***********************************************************************************************/
//Checks if mouse leaves control and changes flag if it does
void dgt_DoControl::OnLeave( wxMouseEvent &event )
{

	m_enter = false;


	Refresh();
}

/**********************************************************************************************/
//checks if control is enable and changes flags if it isn't
bool dgt_DoControl::SetEnable(  )
{
	return TRUE;
}

